<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片试图 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="clearSerach"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="serachBtn"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- table表格区域 -->
      <el-table :data="goodsList" style="width: 100%" border stripe>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="90px">
        </el-table-column>
        <el-table-column prop="goods_weight" label="重量" width="90px">
        </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="150px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="deleteGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="goodstotal"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1, // 当前页码
        pagesize: 5 // 每页显示条数
      },
      goodsList: [], // 商品列表
      goodstotal: 0 // 商品总条数
    }
  },
  methods: {
    async getGooddList() {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败')
      }
      this.goodsList = res.data.goods
      this.goodstotal = res.data.total
      console.log(res.data)
    },
    // 每页条数
    handleSizeChange(newsize) {
      // console.log(`每页 ${newsize} 条`)
      this.queryInfo.pagesize = newsize
      this.getGooddList()
    },
    // 当前页
    handleCurrentChange(newnum) {
      // console.log(`当前页: ${newnum}`)
      this.queryInfo.pagenum = newnum
      this.getGooddList()
    },
    // 搜索功能
    serachBtn() {
      this.getGooddList()
    },
    // 清空搜索
    clearSerach() {
      // console.log('点击了清空')
      this.queryInfo.query = ''
      this.getGooddList()
    },
    // 删除商品列表选择的数据
    async deleteGoods(goodsId) {
      const confirmResult = await this.$confirm('此操作将永久删除本条商品信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch((err) => err)
      if (confirmResult !== 'confirm') {
        return this.$message.error('取消了删除操作')
      }
      const { data: res } = await this.$http.delete(`goods/${goodsId}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除数据失败')
      }
      this.$message.success('删除数据成功')
      this.getGooddList()
    },
    // 跳转到添加页面
    goAddPage() {
      this.$router.push('/goods/add')
    }
  },
  created() {
    this.getGooddList()
  }
}
</script>

<style lang="less" scoped>
.box-card {
  text-align: left;
}
</style>
